package org.kernely.directory;

import java.util.HashSet;
import java.util.Set;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.kernely.core.service.KernelyBundleService;
import org.kernely.directory.userpage.KernelyDirectoryPresenter;
import org.kernely.directory.userpage.KernelyDirectoryView;
import org.kernely.security.service.KernelySecurityService;
import org.kernely.ui.mvp.IKernelyView;
import org.kernely.ui.mvp.KernelyEventBus;
import org.kernely.ui.mvp.KernelyViewPresenterManager;
import org.kernely.ui.mvp.ViewIdentifier;
import org.kernely.ui.services.KernelyUIBundle;
import org.kernely.ui.services.KernelyUIBundleService;
import org.kernely.ui.utils.KernelyBundleMenu;

import com.vaadin.terminal.ExternalResource;
import com.vaadin.terminal.Resource;

/**
 * The bundle of the directory, where users can see other users profiles.
 */
public class DirectoryBundle implements KernelyUIBundle {
	
	/**
	 * The UI registering service
	 */
	private static KernelyUIBundleService uiBundleService;

	/**
	 * The security service : retrieving current user, roles...
	 */
	private static KernelySecurityService securityService;
	
	
	/**
	 * Gets the security service.
	 * @return the security service.
	 */
	public static KernelySecurityService getSecurityService() {
		return securityService;
	}

	public static KernelyEventBus getEventBus() {
		Session session = (Session) SecurityUtils.getSubject().getSession();
		return (KernelyEventBus) session.getAttribute("eventBus");
		
	}
	
	/**
	 * Method called by the Service Component Runtime
	 * 
	 * @param service
	 *            : the KernelyBundleService needed to register this bundle
	 */
	public final void setModuleService(KernelyBundleService service) {
		// Registering the bundle
		service.registerModule(this);
	}

	/**
	 * Method called by the Service Component Runtime
	 * 
	 * @param service
	 *            : the KernelyBundleService needed to unregister this bundle
	 */
	public final void unsetModuleService(KernelyBundleService service) {
		// Unregistering the bundle
		service.unregisterModule(this);
	}

	/**
	 * Sets the KernelyUIBundleService Declarative Service.
	 * 
	 * @param uiBundleService
	 *            : the KernelyUIBundleService service
	 */
	public final void setUIBundleService(KernelyUIBundleService uiBundleService) {
		DirectoryBundle.uiBundleService = uiBundleService;
		DirectoryBundle.uiBundleService.registerModule(this);
	}

	/**
	 * Unsets the KernelyUIBundleService Declarative Service.
	 * 
	 * @param uiBundleService
	 *            : the KernelyUIBundleService service
	 */
	public final void unsetUIBundleService(KernelyUIBundleService service) {
		DirectoryBundle.uiBundleService.unregisterModule(this);
	}
	
	/**
	 * Sets the security Declarative Service
	 * @param service The security service.
	 */
	public final void setSecurityService(KernelySecurityService service) {
		securityService = service;
	}

	/**
	 * Unsets the security Declarative Service
	 * @param service The security service.
	 */
	public final void unsetSecurityService(KernelySecurityService service) {}


	/**
	 * Get the default parent menu item.
	 */
	public final boolean defaultParentMenuItem() {
		// TODO Auto-generated method stub
		return false;
	}

	/**
	 * Get the administration view of the stream bundle.
	 */
	public final Class<? extends IKernelyView> getAdministrationView() {
		return null;
	}

	/**
	 * @see org.kernely.ui.services.KernelyUIBundle#getMenu()
	 */
	public final KernelyBundleMenu getMenu() {
		KernelyBundleMenu menu = new KernelyBundleMenu();
		menu.put("Directory",KernelyDirectoryView.class);
		
		menu.setMenuIcon("Directory", "directorybundle/images/directory16.png");
		return menu;
	}


	/**
	 * @see org.kernely.ui.services.KernelyUIBundle#getViewPresenter()
	 */
	public final KernelyViewPresenterManager getViewPresenter() {
		KernelyViewPresenterManager hm = new KernelyViewPresenterManager();
		// Adding all associations View - Presenter
		hm.put(KernelyDirectoryView.class, KernelyDirectoryPresenter.class);
		return hm;
	}

	/**
	 * Return the name of the stream bundle.
	 * @return the name of the bundle.
	 */
	public final String getName() {
		return "Directory Bundle";
	}
	
	/**
	 * @see org.kernely.ui.mvp.KernelyUIBundle#getIcon()
	 */
	public final Resource getIcon() {
		return new ExternalResource("directorybundle/images/directory32.png");
	}

	/**
	 * @see org.kernely.ui.services.KernelyUIBundle#getFrontPages()
	 */
	public Set<ViewIdentifier> getFrontPages() {
		// Create the set
		HashSet<ViewIdentifier> views = new HashSet<ViewIdentifier>();
		// Add the view
		views.add(new ViewIdentifier("Directory page", (KernelyDirectoryView.class).toString(),this.getName()));
		return views;
	}

}